package com.freewater.springboot.demo.ioc.lookupmethodinject.objectprovider;

import com.freewater.springboot.demo.PackageUtils;
import com.freewater.springboot.demo.ioc.lookupmethodinject.Command;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.support.GenericApplicationContext;

/**
 * 不同生命周期的对象相互依赖：singleton依赖prototype，使用ObjectProvider引用短生命周期的AsyncCommand。
 * 从而组件CommandManager不会依赖Spring框架。
 */
@Slf4j
public class Main {

    public static void main(String[] args) {
        GenericApplicationContext applicationContext = new GenericApplicationContext();
        final String xmlPath = PackageUtils.getDefaultAppContextXmlPath(Main.class);
        new XmlBeanDefinitionReader(applicationContext).loadBeanDefinitions(xmlPath);
        applicationContext.refresh();
        log.info("使用ApplicationContext：{}", xmlPath);
        final CommandManager commandManager = applicationContext.getBean("commandManager", CommandManager.class);
        commandManager.process("hello");
        commandManager.process("world");
        //
        final ObjectProvider<Command> beanProvider = applicationContext.getBeanProvider(Command.class);
        log.info("bean provider returned object: {}", beanProvider.getObject().getClass());
    }
}
